package org.linlinjava.litemall.wx.web;

import lombok.extern.slf4j.Slf4j;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.Vendor;
import org.linlinjava.litemall.db.domain.VendorGoodsTag;
import org.linlinjava.litemall.db.domain.VendorTag;
import org.linlinjava.litemall.db.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 查询商户信息
 */
@Slf4j
@RestController
@RequestMapping("/wx/goods")
public class WxGoodsTagController {
    @Autowired
    private VendorTagService vendorTagService;
    @Autowired
    private LitemallGoodsService litemallGoodsService;

    @Autowired
    private VendorGoodsTagService vendorGoodsTagService;

    /**
     * 查询tag列表
     *
     * @return 测试数据
     */
    @GetMapping("/tags")
    public Object tags(@RequestParam Long vendorId) {
        List<VendorTag> tags = vendorTagService.findByVendorId(vendorId);
        return ResponseUtil.ok(tags);
    }

    /**
     * 传tag列表
     *
     * @return 测试数据
     */
    @GetMapping("/tag_goods")
    public Object tagGoods(@RequestParam Long tagId) {
        List<VendorGoodsTag> tags = vendorGoodsTagService.findByTagId(tagId);
        List<Long> collect = tags.parallelStream().map(VendorGoodsTag::getGoodsId).collect(Collectors.toList());

        List<LitemallGoods> litemallGoods = litemallGoodsService.queryByIds(collect.toArray(new Long[]{}));
        return ResponseUtil.ok(litemallGoods);
    }
}